rm(list=ls(all=TRUE))
library(ggplot2)
library(gridExtra)
library(car)
library(plm)
library(gam)
library(plyr)
library(doBy)
library(foreign)
library(texreg)
library(xtable)
library(MASS)
library(GGally)
library(zoo)
library(lmtest)
library(plm)
library(lattice)
library(ebal)
library(DataCombine)
library(rgenoud)
library(sandwich)
library(Hmisc)
library(dplyr)
library(tidyr)
library(lubridate)
library(zoo)
require(texreg)
require(lmtest)
require(multiwayvcov)
library(VIM)
library(rpart)
setwd("/users/irismalone/Dropbox/AGDPapers/IS-ISSS Paper/Replication Files - II/Data")

df2=read.csv("replication_extsup_data.csv")

df2 = df2 %>% distinct(ccode1, ccode2, torgid, .keep_all = TRUE)

df2$l1noviolsupl = log(df2$l1noviolsup+1)

########################
# HETEROGENEITY ANALYSIS
########################



######
# Table 13
# Only Rebel Groups
######
df3=subset(df2, df2$civwar==1)

reg.m0 = glm((anystatesup) ~ l1noviolsupl + sharedcoideo, 
             data=df3, family="binomial")
vcov_firm <- cluster.vcov(reg.m0, df3$dyadid)
a = coeftest(reg.m0, vcov_firm)
a

reg.m1 = glm((anystatesup) ~ l1noviolsupl*sharedcoideo, 
             data=df3, family="binomial")
vcov_firm <- cluster.vcov(reg.m1, df3$dyadid)
b = coeftest(reg.m1, vcov_firm)
b

reg.m2 = glm((anystatesup) ~ l1noviolsupl*sharedcoideo  + rivalry + minwbgdppc + mindemest, 
             data=df3, family="binomial")
vcov_firm <- cluster.vcov(reg.m2, df3$dyadid)
c = coeftest(reg.m2, vcov_firm)
c


reg.m3 = glm((anystatesup) ~ l1noviolsupl*sharedcoideo + rivalry + minwbgdppc + mindemest + factor(ccode1), 
             data=df3, family="binomial")
vcov_firm <- cluster.vcov(reg.m3, df3$dyadid)
d = coeftest(reg.m3, vcov_firm)
d

texreg(list(a, b, c, d), omit.coef="ccode1", stars=c(0.1, 0.05, 0.01), caption="Rebel Groups")
texreg(list(reg.m0, reg.m1, reg.m2, reg.m3))


htmlreg(list(a, b, c, d), omit.coef="ccode1", stars=c(0.1, 0.05, 0.01), caption="Rebel Groups", file = "table13_rebel_materialsupp.doc")

htmlreg(list(reg.m0, reg.m1, reg.m2, reg.m3), file = "table13_appendix_rebel_unclustered.doc")

######
# Table 14
# Non-Rebel Groups
######
df3=subset(df2, df2$civwar==0)

reg.m0 = glm((anystatesup) ~ l1noviolsupl + sharedcoideo, 
             data=df3, family="binomial")
vcov_firm <- cluster.vcov(reg.m0, df3$dyadid)
a = coeftest(reg.m0, vcov_firm)
a

reg.m1 = glm((anystatesup) ~ l1noviolsupl*sharedcoideo, 
             data=df3, family="binomial")
vcov_firm <- cluster.vcov(reg.m1, df3$dyadid)
b = coeftest(reg.m1, vcov_firm)
b

reg.m2 = glm((anystatesup) ~ l1noviolsupl*sharedcoideo  + rivalry + minwbgdppc + mindemest, 
             data=df3, family="binomial")
vcov_firm <- cluster.vcov(reg.m2, df3$dyadid)
c = coeftest(reg.m2, vcov_firm)
c


reg.m3 = glm((anystatesup) ~ l1noviolsupl*sharedcoideo + rivalry + minwbgdppc + mindemest + factor(ccode1), 
             data=df3, family="binomial")
vcov_firm <- cluster.vcov(reg.m3, df3$dyadid)
d = coeftest(reg.m3, vcov_firm)
d

texreg(list(a, b, c, d), omit.coef="ccode1", stars=c(0.1, 0.05, 0.01), caption="Non-Rebel Groups")
texreg(list(reg.m0, reg.m1, reg.m2, reg.m3))

htmlreg(list(a, b, c, d), omit.coef="ccode1", stars=c(0.1, 0.05, 0.01), caption="Non-Rebel Groups", file = "table14_appendix_nonrebel.doc")

htmlreg(list(reg.m0, reg.m1, reg.m2, reg.m3), file = "table14_appendix_nonrebel_unclustered.doc")

######
# Table 15
# Only Domestic Groups
######

df3=subset(df2, df2$transnationalattackany==0)


reg.m0 = glm((anystatesup) ~ l1noviolsupl + sharedcoideo, 
             data=df3, family="binomial")
vcov_firm <- cluster.vcov(reg.m0, df3$dyadid)
a = coeftest(reg.m0, vcov_firm)
a

reg.m1 = glm((anystatesup) ~ l1noviolsupl*sharedcoideo, 
             data=df3, family="binomial")
vcov_firm <- cluster.vcov(reg.m1, df3$dyadid)
b = coeftest(reg.m1, vcov_firm)
b

reg.m2 = glm((anystatesup) ~ l1noviolsupl*sharedcoideo  + rivalry + minwbgdppc + mindemest, 
             data=df3, family="binomial")
vcov_firm <- cluster.vcov(reg.m2, df3$dyadid)
c = coeftest(reg.m2, vcov_firm)
c


reg.m3 = glm((anystatesup) ~ l1noviolsupl*sharedcoideo + rivalry + minwbgdppc + mindemest + factor(ccode1), 
             data=df3, family="binomial")
vcov_firm <- cluster.vcov(reg.m3, df3$dyadid)
d = coeftest(reg.m3, vcov_firm)
d

texreg(list(a, b, c, d), omit.coef="ccode1", stars=c(0.1, 0.05, 0.01), caption="Domestic Groups")
texreg(list(reg.m0, reg.m1, reg.m2, reg.m3))

htmlreg(list(a, b, c, d), omit.coef="ccode1", stars=c(0.1, 0.05, 0.01), caption="Domestic Groups", file = "table15_appendix_domestic.doc")

htmlreg(list(reg.m0, reg.m1, reg.m2, reg.m3), file = "table15_appendix_domestic_unclustered.doc")


######
# Table 16
# Only Transnational Groups
######

df3=subset(df2, df2$transnationalattackany==1)

reg.m0 = glm((anystatesup) ~ l1noviolsupl + sharedcoideo, 
             data=df3, family="binomial")
vcov_firm <- cluster.vcov(reg.m0, df3$dyadid)
a = coeftest(reg.m0, vcov_firm)
a

reg.m1 = glm((anystatesup) ~ l1noviolsupl*sharedcoideo, 
             data=df3, family="binomial")
vcov_firm <- cluster.vcov(reg.m1, df3$dyadid)
b = coeftest(reg.m1, vcov_firm)
b

reg.m2 = glm((anystatesup) ~ l1noviolsupl*sharedcoideo  + rivalry + minwbgdppc + mindemest, 
             data=df3, family="binomial")
vcov_firm <- cluster.vcov(reg.m2, df3$dyadid)
c = coeftest(reg.m2, vcov_firm)
c


reg.m3 = glm((anystatesup) ~ l1noviolsupl*sharedcoideo + rivalry + minwbgdppc + mindemest + factor(ccode1), 
             data=df3, family="binomial")
vcov_firm <- cluster.vcov(reg.m3, df3$dyadid)
d = coeftest(reg.m3, vcov_firm)
d

texreg(list(a, b, c, d), omit.coef="ccode1", stars=c(0.1, 0.05, 0.01), caption="Transnational Groups")
texreg(list(reg.m0, reg.m1, reg.m2, reg.m3))

htmlreg(list(a, b, c, d), omit.coef="ccode1", stars=c(0.1, 0.05, 0.01), caption="Transnational Groups", file = "table16_appendix_transnational.doc")

htmlreg(list(reg.m0, reg.m1, reg.m2, reg.m3), file = "table16_appendix_transnational_unclustered.doc")


##################
# Table 17
# Alternative Sponsor List - MP
##################
df3=subset(df2, df2$cowmaj2 == 1)

reg.m0 = glm((anystatesup) ~ l1noviolsupl + sharedcoideo, 
             data=df3, family="binomial")
vcov_firm <- cluster.vcov(reg.m0, df3$dyadid)
a = coeftest(reg.m0, vcov_firm)
a

reg.m1 = glm((anystatesup) ~ l1noviolsupl*sharedcoideo, 
             data=df3, family="binomial")
vcov_firm <- cluster.vcov(reg.m1, df3$dyadid)
b = coeftest(reg.m1, vcov_firm)
b

reg.m2 = glm((anystatesup) ~ l1noviolsupl*sharedcoideo  + rivalry + minwbgdppc + mindemest, 
             data=df3, family="binomial")
vcov_firm <- cluster.vcov(reg.m2, df3$dyadid)
c = coeftest(reg.m2, vcov_firm)
c


reg.m3 = glm((anystatesup) ~ l1noviolsupl*sharedcoideo + rivalry + minwbgdppc + mindemest + factor(ccode1), 
             data=df3, family="binomial")
vcov_firm <- cluster.vcov(reg.m3, df3$dyadid)
d = coeftest(reg.m3, vcov_firm)
d

texreg(list(a, b, c, d), omit.coef="ccode1", stars=c(0.1, 0.05, 0.01), caption="Sponsor MP")
texreg(list(reg.m0, reg.m1, reg.m2, reg.m3))

htmlreg(list(a, b, c, d), omit.coef="ccode1", stars=c(0.1, 0.05, 0.01), caption="Sponsor MP", file = "table17_appendix_mpsponsors.doc")

htmlreg(list(reg.m0, reg.m1, reg.m2, reg.m3), file = "table17_appendix_mpsponsors_unclustered.doc")


#######
# Table 18
# Alternative Sponsor List - Remove US and USSR
#######

df3=subset(df2, df2$ccode2 != 2 & df2$ccode2 != 365)

reg.m0 = glm((anystatesup) ~ l1noviolsupl + sharedcoideo, 
             data=df3, family="binomial")
vcov_firm <- cluster.vcov(reg.m0, df3$dyadid)
a = coeftest(reg.m0, vcov_firm)
a

reg.m1 = glm((anystatesup) ~ l1noviolsupl*sharedcoideo, 
             data=df3, family="binomial")
vcov_firm <- cluster.vcov(reg.m1, df3$dyadid)
b = coeftest(reg.m1, vcov_firm)
b

reg.m2 = glm((anystatesup) ~ l1noviolsupl*sharedcoideo  + rivalry + minwbgdppc + mindemest, 
             data=df3, family="binomial")
vcov_firm <- cluster.vcov(reg.m2, df3$dyadid)
c = coeftest(reg.m2, vcov_firm)
c


reg.m3 = glm((anystatesup) ~ l1noviolsupl*sharedcoideo + rivalry + minwbgdppc + mindemest + factor(ccode1), 
             data=df3, family="binomial")
vcov_firm <- cluster.vcov(reg.m3, df3$dyadid)
d = coeftest(reg.m3, vcov_firm)
d

texreg(list(a, b, c, d), omit.coef="ccode1", stars=c(0.1, 0.05, 0.01), caption="Remove Potential Outliers")
texreg(list(reg.m0, reg.m1, reg.m2, reg.m3))

htmlreg(list(a, b, c, d), omit.coef="ccode1", stars=c(0.1, 0.05, 0.01), caption="Transnational Groups", file = "table18_appendix_sovietus.doc")

htmlreg(list(reg.m0, reg.m1, reg.m2, reg.m3), file = "table18_appendix_sovietus_unclustered.doc")

